What is Docker?

Docker is a software platform that allows you to build, test, and deploy applications quickly. Docker packages software into standardized units called containers that have everything the software need to run including libraries, system tools, code, and runtime. Docker provides a consistent and efficient way to package, distribute, and manage applications across different environments.

Imagine you have a new colleague, he/she has to do fussy configurations before running the application — for example, runtime, libraries, and tools. Though it takes much time and effort, the application may finally not run because of the different operating systems and versions. Furthermore, the application may have a different presence in the development environment, test environment and production environment.

Docker can solve these problems because a Docker Container packages everything in it that ensures the same presence on different machines or OS.

Docker vs. Virtual Machine

VM lets you run a virtual machine on any hardware; Docker lets you run an application on any operating system. Docker and VMs both provide an isolated environment. But a VM includes an OS, so it means the VM needs more resources.

VMDocker Container
Boot-timeMinutesSeconds
Runs onExecution engineHypervisor
Process levelShare an OSseparate OS
MemoryGBsKBs/MBs
Recource usageMoreLess

Docker architecture

Docker uses a client-server architecture. The Docker daemon takes the responsibility of building, running, and distributing Docker containers. The Docker client and daemon can run on the same system, or connect a Docker client to a remote Docker daemon. They can communicate using a REST API, over UNIX or s network interface. Another Docker client is Docker Compose, which lets you work with applications consisting of a set of containers.

Docker registries

A docker registry stores Docker images. Docker Hub is a public registry, which is the default registry. we can even run your own private registry. Docker will communicate with the registry when using docker pull, docker run, or docker push.

Docker images

an image is a read-only template with instructions for creating a Docker container. Often, an image is based on another image, with some additional customization.

Docker containers

A container is a runnable instance of an image. We can create several same containers from an image.

Dockerfile

a Dockerfile is a text document that contains the command that instructs how to build a Docker image

Practice: First project

After installing Docker Desktop, We will create a Docker image and a container that runs a super simple Node.js application just say Hello Docker!

Step 1: Create a new project

Create a folder and a Javascript file called index.js inside it.

docker-practice
└─ index.js

The index.js is extremely simple:

console.log('Hello Docker!')

when executing the file, it simply says “Hello Docker!”

Certainly, we can build a Docker image based on an existing project.

Step 2: Add Dockerfile

Add Dockerfile in the root directory

from node:19-alpine
WORKDIR /app
COPY . .
CMD ["node", "index.js"]
EXPOSE 2000
  • from <image>:<version> A Dockerfile must begin with a from instruction that specifies the Parent Image from which we are building. In this case, we use Node runtime.
  • WORKDIR sets the working directory for any RUN, CMD, COPY, and ADD instructions that follow it in the file. This instruction can be used multiple times in a file. If a relative path is provided, it will be relative to the path of the previous WORKDIR instruction.
  • COPY <src> <dest> instruction copies files or directories from src to the filesystem of the container at the path dest. In this case, all the files will be copied to app directory that WORKDIR has instructed.
  • CMD [<executable>, param1, param2] instructs how to execute a container. There can only be one CMD instruction in a file. Must use double quotes.
  • EXPOSE <port> instructor the container listens on the specified network ports at runtime.

See more about Dockerfile here.

Step 3: Build image

docker build -t docker-practice .

-t means to tag the image name. After executing the command above, docker will build an image. It‘s gonna be a little slow the first time.

After building successfully, we can see the image on the Docker Desktop Dashboard.

Or use the command docker images to see the image list:

Step 4: Run a container

docker run -dp 800:2000 docker-practice
# docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  • -d means run container in the background
  • -p means to publish a container’s port to the host — <host_port>:<container_port>

we can see a container on the container list and the application was successfully executed.

Step 5: Share the application

  1. Sign in to Docker Hub and create a new repository
  2. Tag another name that includes the username
docker tag <image_tag> <usernanme/image_tag>

  1. Push the image to Docker Hub
docker push <username>/<image_tag>:<tagname>

we can see our image was successfully pushed

Source Code

https://github.com/roger-twan/docker-practice